$(document).ready(function(){
	setDialog();
	closeDialog();
	//初始化表格
	$('#dataGridTable').datagrid({
		columns:[[ 
           {field:'systemName',title:'系统名称',width:200,align:'center'}, 
           {field:'systemCode',title:'系统编码',width:100,align:'center'},
           {field:'createUser',title:'记录建立者',width:createUserWidth,align:'center'},
           {field:'createTime',title:'记录建立时间',width:createTimeWidth,align:'center'},
           {field:'lastModifiedUser',title:'记录最后修改者',width:lastModifiedWidth,align:'center'},
           {field:'lastModifiedTime',title:'记录最后修改时间',width:createTimeWidth,align:'center'}
         ]],
         onDblClickRow: function (rowIndex,rowData) {  
             var systemId = rowData.systemId; 
             var systemName = rowData.systemName;
             sysMsgShow(systemId+"-"+systemName);
         } 
	});	
	$('#systemName').textbox('textbox').keydown(function (e) {
	   if (e.keyCode == 13) {
		   $('#systemName').textbox('setValue', $(this).val());
		   querySystem();
	    }
	});
	$('#systemCode').textbox('textbox').keydown(function (e) {
		   if (e.keyCode == 13) {
			   $('#systemCode').textbox('setValue', $(this).val());
			   querySystem();
		    }
		});
});

//保存系统
function saveSystem(){
	var systemId=$("#systemInfoDialog_systemId").val();
	var systemName=$("#systemInfoDialog_systemName").val();
	var systemCode=$("#systemInfoDialog_systemCode").val();
	
	if(systemName==null||systemName==''){
		sysMsgShow("系统名称不允许为空!");
		return false;
	}else if(systemCode==null||systemCode==''){
		sysMsgShow("系统编码不允许为空!");
		return false;
	}else{

		$.ajax({
			async : false,
			cache:false,
			type: 'post',
			dataType : "json",
			data : {
				"system.systemId" : systemId,
				"system.systemName" : systemName,
				"system.systemCode" : systemCode
			},
			url: 'sys/saveSysSystem.action',
			error: function () {
				sysErrorShow('请求失败!');
			},success:function(data){
				var messgage = systemId?"修改成功!":"添加成功!";
				if(data.success==true){
					reloadTable();
					closeDialog();
				}else{
					messgage = data.msg;
				}
				sysMsgShow(messgage);
			}
		});
	
	}	
}

//删除系统
function delSystem(){
	if($('#dataGridTable').datagrid('getSelected')){
		var ids = [];
		var cods = [];
		var selectedRow = $('#dataGridTable').datagrid('getSelections');
		for(var i=0;i<selectedRow.length;i++){
			ids.push(selectedRow[i].systemId);
			cods.push(selectedRow[i].systemName);
		}
		var systemId = ids.join(',');
		$.messager.confirm('删除提示', '你确定删除下列系统吗?<br/><br/>'+cods.join(','), function(r){
				if (r){
					var url = 'sys/deleteSysSystem.action?system.systemId='+systemId;
					$.ajax({
						async : false,
						cache:false,
						type: 'post',
						dataType : "json",
						url:url,
						error: function () {
							sysErrorShow('请求失败');
						},success:function(data){
							if(data.success==true){
								sysMsgShow("删除成功!");
							}else{
								sysErrorShow(data.msg);
							}
							reloadTable();
						}
					});
				}
		});
	}
	else
	{
		noSelectData();
	}
}

//查询系统
function querySystem(){
	$("#dataGridTable").datagrid('reload',{		
	 	'system.systemName' : $("#systemName").val(),
	 	'system.systemCode' : $("#systemCode").val()
	}); 
}

/**--------------操作弹出框------------------**/
//初始化模窗
function setDialog(){
	$('#systemInfoDialog').dialog({
		modal: true,
		collapsible : true,
		resizable : true,
		onClose : function(){
			resetDialog();
		}
	});
}

//打开添加或修改模窗
function openDialog_add(){ 
	$('#systemInfoDialog').dialog({
		closed: false,
		modal:true,
        title: "新增系统",
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: saveSystem
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#systemInfoDialog').dialog('close');                    
            }
        }]
    });
}

//打开编辑模窗
function openDialog_edit(){
	if($('#dataGridTable').datagrid('getSelected')){
		var systemId = $('#dataGridTable').datagrid('getSelections')[0].systemId;
		$.getJSON("sys/findSysSystemById.action?system.systemId="+systemId,null,
		function msg(json){
			$("#systemInfoDialog_systemId").val(systemId);
			$("#systemInfoDialog_systemName").textbox('setValue',json.object.systemName);
			$("#systemInfoDialog_systemCode").textbox('setValue',json.object.systemCode);
			openDialog_add();
			$('#systemInfoDialog').dialog('setTitle','编辑系统'); 
		});
	}
	else
	{
		noSelectData();
	}
}

//关闭模窗
function closeDialog(){
	$('#systemInfoDialog').dialog('close');
}

//清空控件值
function resetDialog(){
	$("#systemInfoDialog_systemId").val('');
	$("#systemInfoDialog_systemName").textbox('setValue','');
	$("#systemInfoDialog_systemCode").textbox('setValue','');
}

//刷新表格
function reloadTable(){
	$('#dataGridTable').datagrid('reload');
}

//刷新
function reloadSystem()
{
	$("#systemName").textbox('setValue','');
	querySystem();
}